<?php

namespace App\Exceptions;

use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Support\Facades\Log;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Http\Exceptions\ThrottleRequestsException;
use Laravel\Passport\Exceptions\MissingScopeException;
use Laravel\Passport\Exceptions\OAuthServerException;
use OutOfBoundsException;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Throwable;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        //
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'password',
        'password_confirmation',
    ];

    /**
     * Report or log an exception.
     *
     * @param  \Throwable  $exception
     * @return void
     *
     * @throws \Exception
     */
    public function report(Throwable $exception)
    {
        parent::report($exception);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Throwable  $exception
     * @return \Symfony\Component\HttpFoundation\Response
     *
     * @throws \Throwable
     */
    public function render($request, Throwable $exception)
    {
        // 用户token验证不通过
        if ($exception instanceof AuthenticationException ){
            Log::error($exception->getMessage().':'.$exception->getTraceAsString(),[$request->header('Authorization')]);
            return response()->json([
                'status'=>'323',
                'data'=>null,
//                'msg'=>$exception->getMessage(),
                'msg'=>'you are not authenticated',
            ]);
        }
//
//        // 用户没有对应权限
        if ($exception instanceof MissingScopeException ){
            Log::error($exception->getMessage().':'.$exception->getTraceAsString(),[$request->header('Authorization')]);
            return response()->json([
                'status'=>'324',
                'data'=>null,
                'msg'=>$exception->getMessage(),
            ]);
        }
//
//        // 用户token作废
        if ($exception instanceof OAuthServerException ){
            Log::error($exception->getMessage().':'.$exception->getTraceAsString(),[$request->header('Authorization')]);
            return response()->json([
                'status'=>'323',
                'data'=>null,
//                'msg'=>$exception->getMessage(),
                'msg'=>'The resource owner or authorization server denied the request',
            ]);
        }
//
//        // 用户token格式不正确
        if ($exception instanceof OutOfBoundsException ){
            Log::error($exception->getMessage().':'.$exception->getTraceAsString(),[$request->header('Authorization')]);
            return response()->json([
                'status'=>'323',
                'data'=>null,
//                'msg'=>$exception->getMessage(),
                'msg'=>'The resource owner or authorization server denied the request',
            ]);
        }
//
        // ThrottleRequestsException 请求太过频繁
        if ($exception instanceof ThrottleRequestsException ){
            $msg = $exception->getTrace();
            array_splice($msg, 3);
            Log::warning($exception->getMessage(),$msg);
            return response()->json([
                'status'=>'406',
                'data'=>null,
//                'msg'=>$exception->getMessage(),
                'msg'=>'请求太过频繁！',
            ]);
        }
        // MethodNotAllowedHttpException 请求方式错误
        if ($exception instanceof MethodNotAllowedHttpException ){
            Log::warning($exception->getMessage(),$exception->getHeaders());
            return response()->json([
                'status'=>'405',
                'data'=>null,
                'msg'=>$exception->getMessage()
            ]);
        }
//        // 运行时错误
        if($exception instanceof \RuntimeException){
            Log::error($exception->getMessage().':'.$exception->getTraceAsString());
//            WorkGroupWeChar::COMMON($exception->getMessage().':'.$exception->getTraceAsString());
            return response()->json([
                'status'=>'500',
                'data'=>null,
                'msg'=>$exception->getMessage(),
            ],500);
        }
//
        // 错误返回
        if($exception instanceof \Exception){
            Log::error($exception->getMessage().':'.$exception->getTraceAsString());
//            WorkGroupWeChar::COMMON($exception->getMessage().':'.$exception->getTraceAsString());
            return response()->json([
                'status'=>'502',
                'data'=>null,
                'msg'=>$exception->getMessage(),
            ],502);
        }

        return parent::render($request, $exception);
    }
}
